package org.dxy.trigger.listener;

import com.google.common.eventbus.Subscribe;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.domain.event.equipment.EquipmentRegisteredEvent;
import org.dxy.infrastructure.entity.query.EquipmentBasicViewEntity;
import org.dxy.infrastructure.persistent.service.EquipmentBasicViewService;
import org.springframework.stereotype.Component;


/**
 * @description: 监听设备激活事件，并将设备状态更新事件发布到事件总线
 * 设备激活后，发布设备状态变更事件，用于更新设备状态视图或其他相关逻辑
 * @author: dxy
 * @createTime: 2025/6/16 0:18
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class EquipmentRegisteredEventListener implements EventListener<EquipmentRegisteredEvent>{

    private final EquipmentBasicViewService equipmentBasicViewService;


    /**
     * 处理设备激活事件
     * @param event 设备激活事件，包含设备ID等信息
     */

    @Override
    @Subscribe
    public void onEvent(EquipmentRegisteredEvent event) {
        log.info("接收到设备注册事件,event：{}", event.toString());
        EquipmentBasicViewEntity equipmentBasicViewEntity = new EquipmentBasicViewEntity();
        equipmentBasicViewEntity.setId(event.getEquipmentId())
                .setEquipmentType(event.getEquipmentType())
                .setSerialNumber(event.getSerialNumber());
        boolean updated = equipmentBasicViewService.updateById(equipmentBasicViewEntity);
        if (updated) {
            log.info("设备读模型更新成功，设备ID={}", event.getEquipmentId());
        } else {
            log.warn("设备读模型更新失败，设备ID={}", event.getEquipmentId());
        }
    }

}
